<template>
  <div class="admin_page">
    <admin-header></admin-header>
    <admin-side-bar></admin-side-bar>
    <admin-container>
      <el-form inline size="mini" :model="form">
        <el-form-item>
          <el-input
            v-model="form.name"
            prefix-icon="el-icon-search"
            placeholder="标题"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="success">
            搜索
          </el-button>
          <el-button type="primary" @click="() => $refs.add.show()">
            添加
          </el-button>
          <el-button type="primary" @click="() => $refs.cache.show()">
            更新数据
          </el-button>
          <el-button
            type="primary"
            @click="
              () =>
                $router.push({
                  path: '/admin/cache',
                  query: { key: 'cate', from: $route.fullPath }
                })
            "
          >
            清理缓存
          </el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="appLoading"
        border
        size="mini"
        :data="data.data"
        row-key="id"
        lazy
        :load="load"
        :tree-props="{ children: 'children', hasChildren: 'has_children' }"
      >
        <el-table-column label="ID" prop="id"></el-table-column>
        <el-table-column label="名称">
          <template slot-scope="{ row }">
            <a
              target="_blank"
              :href="row.url || '/home/news?cate_id=' + row.id"
            >
              {{ row.name }}
            </a>
            &nbsp;&nbsp;
            <router-link :to="'/admin/news/list?cate_id=' + row.id">
              相关新闻
            </router-link>
          </template>
        </el-table-column>
        <el-table-column label="父节点" prop="fname"></el-table-column>
        <el-table-column label="URL" prop="url"></el-table-column>
        <el-table-column label="标识" prop="title"></el-table-column>
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="{ row }">
            <el-button
              type="text"
              size="mini"
              @click="
                $refs.add.show({
                  fid: row.id
                })
              "
            >
              添加子节点
            </el-button>
            <el-button type="text" size="mini" @click="$refs.add.show(row)">
              修改
            </el-button>
            <el-button type="text" size="mini" @click="del(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="mt10 bg_fff">
        <el-pagination
          :total="page.total"
          :page-size="page.pageSize"
          :current-page="page.pageNum"
          @current-change="pageChange"
        ></el-pagination>
      </div>
    </admin-container>
    <news-cate-add-dialog
      ref="add"
      @ok="() => $refs.cache.show()"
    ></news-cate-add-dialog>
    <news-cate-cache-update-dialog
      ref="cache"
      @ok="refresh"
    ></news-cate-cache-update-dialog>
  </div>
</template>

<script>
import AdminHeader from "../../../components/admin/header";
import AdminSideBar from "../../../components/admin/side-bar";
import AdminContainer from "../../../components/admin/container";
import { AppLoadingMixin } from "../../../lib/mixins/app-loading";
import { PageMixin } from "../../../lib/mixins/PageMixin";
import NewsCateAddDialog from "../../../components/news/cate-add-dialog";
import NewsCateCacheUpdateDialog from "../../../components/news/cate-cache-dialog";

export default {
  name: "Cate",
  components: {
    NewsCateCacheUpdateDialog,
    NewsCateAddDialog,
    AdminContainer,
    AdminSideBar,
    AdminHeader
  },
  mixins: [AppLoadingMixin, PageMixin],
  data() {
    return {
      form: { fid: 0, module: "news" }
    };
  },
  computed: {},
  mounted() {
    this.getData();
  },
  methods: {
    del(data) {
      this.$confirm(`是否删除${data.name}?`).then(async () => {
        if (await this.$cateService.del(data.id + "")) {
          this.calculatePage("-");
          this.getData();
        }
      });
    },
    async load(tree, treeNode, resolve) {
      const data = await this.$cateService.list({
        fid: tree.id,
        pageSize: 10000
      });
      resolve(data.data || []);
    },
    async getData() {
      const { page, form } = this;
      this.data.data.splice(0);
      this.data = await this.$cateService.list({ ...page, ...form });
    }
  }
};
</script>

<style scoped></style>
